package com.pgmmers.radar.dal.model.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pgmmers.radar.dal.bean.ActivationQuery;
import com.pgmmers.radar.dal.bean.PageResult;
import com.pgmmers.radar.dal.model.ActivationDal;
import com.pgmmers.radar.mapper.ActivationMapper;
import com.pgmmers.radar.mapstruct.ActivationMapping;
import com.pgmmers.radar.model.ActivationPO;
import com.pgmmers.radar.util.BaseUtils;
import com.pgmmers.radar.vo.model.ActivationVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class ActivationDalImpl implements ActivationDal {

    public static Logger logger = LoggerFactory.getLogger(ActivationDalImpl.class);

    @Autowired
    private ActivationMapper activationMapper;
    @Resource
    private ActivationMapping activationMapping;

    @Override
    public ActivationVO get(Long id) {
        ActivationPO activation = activationMapper.selectById(id);
        if (activation != null) {
            return activationMapping.sourceToTarget(activation);
        }
        return null;
    }

    @Override
    public PageResult<ActivationVO> query(ActivationQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());

        LambdaQueryWrapper<ActivationPO> wrapper = Wrappers.lambdaQuery(ActivationPO.class);

        wrapper.eq(ActivationPO::getModelId, query.getModelId());
        if (!StringUtils.isEmpty(query.getName())) {
            wrapper.like(ActivationPO::getActivationName, BaseUtils.buildLike(query.getName()));
        }
        if (query.getStatus() != null) {
            wrapper.eq(ActivationPO::getStatus, query.getStatus());
        }

        PageInfo<ActivationPO> page = new PageInfo<>(activationMapper.selectList(wrapper));
        List<ActivationVO> listVO = activationMapping.sourceToTarget(page.getList());

        return new PageResult<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), listVO);
    }

    @Override
    public int save(ActivationVO activation) {
        ActivationPO activationPO = activationMapping.targetToSource(activation);
        Date sysDate = new Date();
        int count = 0;
        if (activation.getId() == null) {
            activationPO.setCreateTime(sysDate);
            activationPO.setUpdateTime(sysDate);
            count = activationMapper.insert(activationPO);
            activation.setId(activationPO.getId());// 返回id
        } else {
            activationPO.setUpdateTime(sysDate);
            count = activationMapper.updateById(activationPO);
        }
        return count;
    }

    @Override
    public int delete(Long[] id) {
		int count = activationMapper.delete(
				Wrappers.lambdaQuery(ActivationPO.class).in(ActivationPO::getId,Arrays.asList(id)));
        // TODO 删除关联子表
        return count;
    }

}
